package tree.递归;

import po.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 二叉树的所有路径257.java
 * @Description https://leetcode-cn.com/problems/binary-tree-paths/
 * @createTime 2021年04月19日 13:03:00
 */
public class 二叉树的所有路径257 {

    List<String> list=new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) {
            return list;
        }
        dfs(root,"");
        return list;
    }

    private void dfs(TreeNode root,String path) {
        //节点不为空则继续
        if (root!=null){
            StringBuilder builder= new StringBuilder(path);
            builder.append(root.val);
            //为叶子节点
            if (root.left == null && root.right == null) {
                list.add(builder.toString());
            }else {
                builder.append("->");
                dfs(root.left,builder.toString());
                dfs(root.right,builder.toString());
            }
        }
    }
}
